package edu.gdkm.dao;

import edu.gdkm.pojo.UserSubscription;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserSubscriptionDao {

    // 根据ID查询
    @Select("SELECT * FROM UserSubscriptions WHERE subscription_id = #{subscriptionId}")
    UserSubscription selectById(Integer subscriptionId);

    // 根据用户ID查询
    @Select("SELECT * FROM UserSubscriptions WHERE user_id = #{userId}")
    List<UserSubscription> selectByUserId(Integer userId);
    
    // 查询用户当前有效的订阅
    @Select("SELECT * FROM UserSubscriptions WHERE user_id = #{userId} AND status = 'active'")
    UserSubscription selectActiveByUserId(Integer userId);

    // 查询所有
    @Select("SELECT * FROM UserSubscriptions")
    List<UserSubscription> selectAll();

    // 新增
    @Insert("INSERT INTO UserSubscriptions(user_id, plan_id, start_date, end_date, status, payment_method, " +
            "last_payment_date, next_payment_date) " +
            "VALUES(#{userId}, #{planId}, #{startDate}, #{endDate}, #{status}, #{paymentMethod}, " +
            "#{lastPaymentDate}, #{nextPaymentDate})")
    @Options(useGeneratedKeys = true, keyProperty = "subscriptionId")
    int insert(UserSubscription userSubscription);

    // 更新
    @Update("UPDATE UserSubscriptions SET user_id = #{userId}, plan_id = #{planId}, start_date = #{startDate}, " +
            "end_date = #{endDate}, status = #{status}, payment_method = #{paymentMethod}, " +
            "last_payment_date = #{lastPaymentDate}, next_payment_date = #{nextPaymentDate} " +
            "WHERE subscription_id = #{subscriptionId}")
    int update(UserSubscription userSubscription);

    // 删除
    @Delete("DELETE FROM UserSubscriptions WHERE subscription_id = #{subscriptionId}")
    int delete(Integer subscriptionId);
}
    